package com.jiao.controller;

import com.jiao.bean.TBao;
import com.jiao.bean.TBaoQuery;
import com.jiao.bean.vo.JsonObjVo;
import com.jiao.bean.vo.PageBeanVo;
import com.jiao.service.TBaoService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 参保人查询
 */
@RestController
@RequestMapping("/insuredman")
@Log
public class TBaoController {

    @Autowired
    TBaoService tBaoService;

    @GetMapping(value = "/findone")
    JsonObjVo query(@RequestParam int id){
        TBao tBao = tBaoService.queryById(id);
        if (null != tBao){
            return JsonObjVo.successReponse(tBao);
        }
        return JsonObjVo.successReponseMsg("没有查询到匹配的数据！");
    }

    @GetMapping(value = "/find")
    JsonObjVo queryPage(@RequestParam int year,int month,int pageIndex,int pageSize){
        TBaoQuery tBaoQuery = new TBaoQuery();
        tBaoQuery.createCriteria().andYearEqualTo(year).andMonthEqualTo(month);
        tBaoQuery.createCriteria().andStateEqualTo(1);
        int pageNo = pageIndex + 1;
        tBaoQuery.setPageNo(pageIndex+1);
        tBaoQuery.setPageSize(pageSize);
        List<TBao> list = tBaoService.query(tBaoQuery);
        if (list.size() > 0 ){
            PageBeanVo pageBeanVo = new PageBeanVo();
            pageBeanVo.setPageIndex(pageNo);
            pageBeanVo.setPageSize(pageSize);
            pageBeanVo.setTotal(list.size());
            pageBeanVo.setList(list);
            return JsonObjVo.successReponse(pageBeanVo);
        }
        return JsonObjVo.successReponseMsg("没有查询到匹配的数据！");
    }

    @PostMapping("/add")
    JsonObjVo insert(@Valid @RequestBody TBao tBao){
        int flag = tBaoService.insert(tBao);
        if (flag > 0){
            return JsonObjVo.successReponse("数据插入成功！");
        }
        return JsonObjVo.failReponse("网络异常，请稍后重试");
    }


    @PostMapping("/modify")
    JsonObjVo update(@RequestBody TBao tBao){
        TBao selectBao = tBaoService.queryById(tBao.getId());
        if (null == selectBao){
            return JsonObjVo.successReponseMsg("没有查询到匹配的数据！");
        }
        if (null != tBao.getCity()){
            selectBao.setCity(tBao.getCity());
        }
        if (null != tBao.getStatus()){
            selectBao.setStatus(tBao.getStatus());
        }
        if (null != tBao.getCompany()){
            selectBao.setCompany(tBao.getCompany());
        }
        int flag = tBaoService.update(selectBao);
        if (flag > 0){
            return JsonObjVo.successReponse("数据修改成功!");
        }
        return JsonObjVo.failReponse("网络异常，请稍后重试");
    }

    @GetMapping("/remove")
    JsonObjVo delete(@RequestParam Integer id){
        TBao tBao = tBaoService.queryById(id);
        if (null == tBao){
            return JsonObjVo.successReponseMsg("没有查询到匹配的数据！");
        }
        tBao.setState(0);
        Integer flag = tBaoService.update(tBao);
        if (flag > 0){
            return JsonObjVo.successReponse("移除成功！");
        }
        return JsonObjVo.failReponse("网络异常，请稍后重试");
    }


}
